<?php
namespace app\wxapp\controller;

use app\model\Theme; 
use app\model\Points; 

class LtPoints extends Base
{
    public function Lt_points()
    {
/**
 * Created by 易宣
 * Desc:获取用户积分排名以及所有用户的积分排名 分页 获取用户可以增加积分列表
 * User: cbw
 * Date: 2020/11/28
 */

global $_P, $_G;
$response = modelShow(40000);
if (!$_G['ispost']){
    $response = modelShow(50000);
}else{
    $fans = $this->checkuser();
    $system=$_G['system'];
    $params = $_P;
    $page_index = max(1,intval($_P['page']));
    $page_size = 10;
    $list =[];
    $themeid = isset($params['themeid'])?$params['themeid']:'';
    if (!$themeid || empty($fans) || empty($system)){
        $response = modelShow(40001);
    }else{
        $theme = Theme::getThemeById($themeid);
        $config =   Theme::getConfig($themeid);
        if (!$theme){
            $response = modelShow(40002);
        }else{
            try {
                $page_total = intval(Points::getAllUserByPointsEqZero()['cnt']);
                if ($page_total>0){
                    $list = Points::getAllUserPointsRanking($page_index,$page_size);
                }
                $user_self_points_ranking = Points::getUserPointsRanking($fans['uid'],$themeid)['cnt'];
                $self_points_flag = formatSelfPointsFlag(Points::getUserChancePointsFlag($fans['uid'],$themeid,20));
                $op_points_list = Points::getUserPointsOpList($fans['uid'],$themeid,$config);

                $config_new = array(
                    'points_isshow' => $config ['points_isshow'],
                    'pointsimgs'=>$config['pointsimgs'],
                    'points_rank_in_img'=>$config['points_rank_in_img'],
                );
                $data = [
                    'config'=>$config_new,
                    'all_user_points_ranking'=>$list,
                    'user_self_points_ranking'=>$user_self_points_ranking,
                    'page_index'=>$page_index,
                    'page_total'=>$page_total,
                    'page_max'=>ceil($page_total/$page_size),
                    'self_points'=>$fans['lt_points'],
                    'self_avatar'=>$fans['avatar'],
                    'self_nickname'=>$fans['nickname'],
                    'self_points_flag'=>$self_points_flag,
                    'list'=>$op_points_list,
                    'before_time'=>date("Y-m-d",$config['first_buy_endtime']),
                    'islottery'=>Points::getCheckLotteryOrPoints('lottery',$config,$system),
                    'qr_img'=>$config['qr_img'],
                    'points_status'=>Points::getCheckLotteryOrPoints('points',$config,$system)
                ];
                $response = modelShow(200,'',$data);
            }catch (Exception $e){
                logging_run('错误日志:GetPointsRanking[参数：'.json_encode($params).'-错误信息:'.$e->getMessage());
            }
        }
    }
}
wxapp_result(0, '',$response);

}
}